package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.CreateAccountPresenter;

public class CreateAccountView extends Activity implements View.OnClickListener {
	private String PACKAGE_NAME;
	private CreateAccountPresenter myPresenter;
	private Button bCreateAccount;
	private Button bAlreadyHaveAccount;
	private EditText etEmail;
	private EditText etPassword;
	private EditText etConfirmPassword;
	private EditText etPhoneNumber;
	private TextView tvCreateAccountResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_account);
		PACKAGE_NAME = getApplicationContext().getPackageName();

		linkSetup();
		myPresenter = new CreateAccountPresenter(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bCreateAccount:
			String email = etEmail.getText().toString();
			String password = etPassword.getText().toString();
			String confirmPassword = etConfirmPassword.getText().toString();
			String phoneNumber = etPhoneNumber.getText().toString();
			myPresenter.createAccount(email, password, confirmPassword,
					phoneNumber);
			break;
		case R.id.bHaveAccountAlready:
			myPresenter.toLogin();
			break;
		}

	}

	public String getPackageName() {
		return PACKAGE_NAME;
	}

	/**
	 * Displays a confirmation message
	 * @param result the message to display
	 */
	public void setCreateAccountResult(String result) {
		tvCreateAccountResult.setText(result);
		tvCreateAccountResult.setTextColor(Color.RED);
	}

	/**
	 * Initializes all the views in the XML layout, and sets listeners for the buttons
	 */
	private void linkSetup() {
		bCreateAccount = (Button) findViewById(R.id.bCreateAccount);
		bAlreadyHaveAccount = (Button) findViewById(R.id.bHaveAccountAlready);
		etEmail = (EditText) findViewById(R.id.etEmail);
		etPassword = (EditText) findViewById(R.id.etPassword);
		etConfirmPassword = (EditText) findViewById(R.id.etConfirmPassword);
		etPhoneNumber = (EditText) findViewById(R.id.etPhoneNumber);
		tvCreateAccountResult = (TextView) findViewById(R.id.tvCreateAccountResult);

		bCreateAccount.setOnClickListener(this);
		bAlreadyHaveAccount.setOnClickListener(this);
	}
}
